-module ( bug ).
-export ( [start/0] ).

-record ( vec3, { x=0, y=0, z=0 } ).


vec3ma( V0, V1, K ) when is_record( V0, vec3 ) and is_record( V1, vec3 ) and is_number( K ) ->
  #vec3{ x = V0#vec3.x + V1#vec3.x * K, y = V0#vec3.y + V1#vec3.y * K, z = V0#vec3.z + V1#vec3.z * K }.


makeVecTestList( L, 0 ) -> L;
makeVecTestList( L, N ) ->
  E = { #vec3{ x = N * 10.0, y = 20.0, z = 30.0 }, #vec3{ x=10.0, y=20.0, z=-40.0 } },
  makeVecTestList( [ E|L ], N - 1 ).


proc( L ) -> proc( L, [] ).
proc( [], Acc ) -> Acc;
proc( [ { P, V } | T ], Acc ) -> proc( T, [ { vec3ma( P, V, 1.2 ), V } | Acc ] ).


vecTestLoop( L, T, I ) ->
  Now = now(),
  DT = timer:now_diff( Now, T ) * 1.0e-3,
  io:format( "Step~p: ~p ms\n", [I, DT] ),
  vecTestLoop( proc( L ), Now, I ).


start() ->
  L = makeVecTestList( [], 1000000 ),
  io:format( "Running test with: ~p\n", [length(L)] ),
  spawn( fun() -> vecTestLoop( L, now(), 1 ) end ),
  spawn( fun() -> vecTestLoop( L, now(), 2 ) end ).
